﻿@model dynamic
           
@{
    CmsPage currentPage = Model.Page;
    bool disabled = Model.Disabled;    
    string selectElementId = Model.ClientId;

    ResFile = "cms_Pages";
}

@functions
{
    string IsDisabled(bool isDisabled)
    {
        return isDisabled ?
            "disabled=disabled" :
            string.Empty;
    }
    string IsSelected(CmsPage page, CmsPage currentPage)
    {
        return page.Id.Equals(currentPage.Id) ?
            "selected=selected" :
            string.Empty;
    }
    string OptionTitle(CmsPage page, CmsPage currentPage, IEnumerable<CmsPageContainer> pages)
    {                
        string result = _("Don't change location");
        
        if(!page.Id.Equals(currentPage.Id))
        {
            result = _("Locate {0} {1}", 
                pages.FindElement(page) > pages.FindElement(currentPage) ? _("after") : _("before"),
                page.PageName);
        }
        
        return result;
    }
    int FirstPageId(IEnumerable<CmsPage> pages)
    {
        return pages.First().Id;
    }
    int LastPageId(IEnumerable<CmsPage> pages)
    {
        return pages.Last().Id;
    }
}

@(Html.CmsAdminPageLocation(currentPage).Wrapper("PageLocationContainer").Template(
    @<text>    
    @{var pages = item.Select(p => p.Page);}
    <select id="@selectElementId" name="@selectElementId" @IsDisabled(disabled)>
        <optgroup label="Special">
            <option value="@FirstPageId(pages)">@_("Locate first")</option>
            <option value="@LastPageId(pages)">@_("Locate last")</option>
        </optgroup>
        <optgroup label="Pages">        
        @foreach(var page in pages)
        {
            <option value="@page.Id" @IsSelected(page, currentPage)>@OptionTitle(page, currentPage, item)</option>
        }  
        </optgroup>
    </select>
    </text>)
    .NoElementsTemplate(
    @<text>
    @_("First")
    </text>
    )
        .Render())